﻿using System;
using System.Collections.Generic;
using System.Linq;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Areas.Shop;
using ASPNETMVCApplication.Repositories.Core;

namespace ASPNETMVCApplication.Repositories
{
    /// <summary>
    /// Supplier Repository.
    /// </summary>
    public class SupplierRepository : RepositoryBase, ISupplierRepository
    {
        public List<Supplier> GetList(Criterion criterion = null)
        {
            var request = new SupplierRequest().Prepare();

            request.LoadOptions = new string[] { "Suppliers" };
            request.Criteria = new SupplierCriteria
            {
                SupplierId=0,
                SortExpression = "ASC"
            };

            var response = Client.GetSuppliers(request);

            Correlate(request, response);

            return response.Suppliers == null ? null : response.Suppliers.ToList();
        }

        public Supplier Get(int _supplierId)
        {
            var request = new SupplierRequest().Prepare();
            request.LoadOptions = new string[] { "Supplier" };
            request.Criteria = new SupplierCriteria { SupplierId = _supplierId };

            var response = Client.GetSuppliers(request);

            Correlate(request, response);

            return response.Supplier;
        }
        public List<Supplier> Search(int _supplierID)
        {
            var request = new SupplierRequest().Prepare();

            request.LoadOptions = new string[] { "Search" };

            request.Criteria = new SupplierCriteria
            {
                SupplierId = _supplierID
            };

            var response = Client.GetSuppliers(request);

            Correlate(request, response);

            return response.Suppliers.ToList();
        }

        //Spplier login 
        public List<Supplier> Login(string userName,string passWord)
        {
            var request = new SupplierRequest().Prepare();

            request.LoadOptions = new string[] { "Login" };

            request.Criteria = new SupplierCriteria
            {
                UserName=userName,PassWord=passWord
            };

            var response = Client.GetSuppliers(request);

            Correlate(request, response);

            return response.Suppliers.ToList();
        }
        public int GetCount(Criterion _criterion = null)
        {
            throw new NotImplementedException();
        }
        public void Insert(Supplier _supplier)
        {
            var request = new SupplierRequest().Prepare();
            request.Action = "Create";
            request.Supplier = _supplier;

            var response = Client.SetSuppliers(request);

            Correlate(request, response);

            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
        }
        public void Update(Supplier _supplier)
        {
            var request = new SupplierRequest().Prepare();
            request.Action = "Update";
            request.Supplier = _supplier;

            var response = Client.SetSuppliers(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
        }
        public void Delete(int _supplierID)
        {
            var request = new SupplierRequest().Prepare();
            request.Action = "Delete";
            request.SupplierID = _supplierID;

            var response = Client.SetSuppliers(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
        }
    }
   
}